#include <Arduino.h>
#include "esp_camera.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "mycam.h"
#include "mymqtt.h"
#include "esp_sleep.h"
#include "nvs_flash.h"
#include "nvs.h"
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 8        
#define PMOS_CTL_PIN GPIO_NUM_7

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
HardwareSerial LDSerial(1);  // 串口1 (UART1)
const uint8_t enableConfigCmd[] ={0xFD,0xFC,0xFB,0xFA,0x04,0x00,0xFF,0x00,0x01,0x00,0x04,0x03,0x02,0x01};
const uint8_t enableEngineeringModeCmd[] ={0xFD,0xFC,0xFB,0xFA,0x02,0x00,0x62,0x00,0x04,0x03,0x02,0x01};
const uint8_t disableConfigCmd[] ={0xFD,0xFC,0xFB,0xFA,0x02,0x00,0xFE,0x00,0x04,0x03,0x02,0x01};
// 全局变量定义
esp_err_t ret;
nvs_handle_t my_handle;
TaskHandle_t captureTask_handle =NULL;
TaskHandle_t uartTask_handle =NULL;
uint32_t cam_time=0;
<<<<<<< HEAD
int32_t boot_count3;

=======
int32_t boot_count;
int32_t boot_count2;
<<<<<<< HEAD
int32_t boot_count4;
=======
>>>>>>> 4043307f87f6dd520e3c6700771cc5863acdd976

>>>>>>> ca32db4162330693baee5c34e16f94d5ae303f02
void setup() {
    Serial.begin(3000000);  // 初始化串口
    Serial.setDebugOutput(true);
    Serial.println();
    LDSerial.begin(256000,SERIAL_8N1,10,11);
    pinMode(PMOS_CTL_PIN, OUTPUT);  //PMOS
    digitalWrite(PMOS_CTL_PIN, LOW);
    pinMode(IR_PIN, INPUT_PULLDOWN);
    pinMode(ONE_WIRE_BUS, INPUT_PULLUP); // DS18B20
    sensors.begin();
    camera_init();// 初始化摄像头

    LDSerial.write(enableConfigCmd, sizeof(enableConfigCmd));// 初始化雷达
    LDSerial.write(enableEngineeringModeCmd, sizeof(enableEngineeringModeCmd));
    while (LDSerial.available()) {
      LDSerial.read();
    }
    LDSerial.write(disableConfigCmd, sizeof(disableConfigCmd));	
    // 初始化NVS
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        // 如果NVS分区被截断或版本不匹配，则擦除NVS分区并重新初始化
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    // 打开NVS命名空间
    ret = nvs_open("storage", NVS_READWRITE, &my_handle); 
    if (ret != ESP_OK) {
        Serial.println("Error opening NVS handle!");
    } else {
        Serial.println("NVS handle opened successfully!");
    }

    //读取开机次数，如果没有键值初始化0				
	  ret = nvs_get_i32(my_handle, "boot_count", &boot_count);
	  if (ret!= ESP_OK) {
			boot_count = 0;
	  }
	  boot_count++;
    Serial.print("boot_count:");
    Serial.println(boot_count);

      // 发送使能配置命令
  // if (LDsendCommand(enableConfigCmd, sizeof(enableConfigCmd))) {
  //   Serial.println("Enable config command ACK received");
  //   // 发送使能工程模式命令
  //   if (LDsendCommand(enableEngineeringModeCmd, sizeof(enableEngineeringModeCmd))) {
  //     Serial.println("Enable engineering mode command ACK received");
  //     // 发送关闭使能命令
  //     if (LDsendCommand(disableConfigCmd, sizeof(disableConfigCmd))) {
  //       Serial.println("Disable config command ACK received");
  //     } else {
  //       Serial.println("Failed to receive ACK for disable config command");
  //     }
  //   } else {
  //     Serial.println("Failed to receive ACK for enable engineering mode command");
  //   }
  // } else {
  //   Serial.println("Failed to receive ACK for enable config command");
  // }



  // // 创建任务：图像捕获任务
  xTaskCreate(captureTask, "Capture Task", 8192*2, NULL, 1, &captureTask_handle);


  // // 创建任务：4g mqtt接收线程
  xTaskCreate(UARTTask, "UART Task", 8192*2, NULL, 1, &uartTask_handle);

  // // 创建任务：图像发送任务
  xTaskCreate(MQTTTask, "MQTT Task", 8192*2, NULL, 1, NULL);

  xTaskCreate(checkSleepTask, "Check Sleep Task", 2048, NULL, 1, NULL);
  Serial.println("FreeRTOS任务已启动");
}



void loop() {
  // 主循环不需要处理任何任务，所有工作由 FreeRTOS 任务处理
  vTaskDelay(100 / portTICK_PERIOD_MS);

}